#!/usr/bin/env python
# @author: Samuel Taylor <samtaylor.uk@gmail.com>
# @package: winecellar
# @license: GNU GPLv3
#
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010  Samuel Taylor <samtaylor.uk@gmail.com>
#----------------------------------------------------------------------
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------

__author__ = 'Samuel Taylor <samtaylor.uk@gmail.com>'

import os
import sys

try:
    import DistUtilsExtra.auto
except ImportError:
    print >> sys.stderr, 'To build test you need https://launchpad.net/python-distutils-extra'
    sys.exit(1)
assert DistUtilsExtra.auto.__version__ >= '2.18', 'needs DistUtilsExtra.auto >= 2.18'

def update_data_path(prefix, oldvalue=None):

    try:
        fin = file('WineCellar/Config.py', 'r')
        fout = file(fin.name + '.new', 'w')

        for line in fin:            
            fields = line.split(' = ') # Separate variable from value
            if fields[0] == '__dataPath__':
                # update to prefix, store oldvalue
                if not oldvalue:
                    oldvalue = fields[1]
                    line = "%s = '%s'\n" % (fields[0], prefix)
                else: # restore oldvalue
                    line = "%s = %s" % (fields[0], oldvalue)
            fout.write(line)

        fout.flush()
        fout.close()
        fin.close()
        os.rename(fout.name, fin.name)
    except (OSError, IOError), e:
        print ("ERROR: Can't find WineCellar/Config.py")
        sys.exit(1)
    return oldvalue


def update_desktop_file(datadir):

    try:
        fin = file('winecellar.desktop.in', 'r')
        fout = file(fin.name + '.new', 'w')

        for line in fin:            
            if 'Icon=' in line:
                line = "Icon=%s\n" % (datadir + '/winecellar.png')
            fout.write(line)
        fout.flush()
        fout.close()
        fin.close()
        os.rename(fout.name, fin.name)
    except (OSError, IOError), e:
        print ("ERROR: Can't find winecellar.desktop.in")
        sys.exit(1)


class InstallAndUpdateDataDirectory(DistUtilsExtra.auto.install_auto):
    def run(self):
        print self.prefix
        previous_value = update_data_path(self.prefix + '/local/share/WineCellar/')
        update_desktop_file(self.prefix + '/local/share/WineCellar/')
        DistUtilsExtra.auto.install_auto.run(self)
        update_data_path(self.prefix, previous_value)

class Remove(DistUtilsExtra.auto.install_auto):
    def run(self):
        print "Removing"        

DistUtilsExtra.auto.setup(name = "WineCellar",
    version = "pre0.1",
    description = "WineCellar helps you install and run applications and games designed for Microsoft Windows",
    author = "Samuel Taylor",
    author_email = "samtaylor.uk@gmail.com",
    url = "http://wine-cellar.googlecode.com",
    license='GPL-3',
    packages = ['WineCellar'],
    long_description = """WineCellar uses online repositories so that applications and games can be added to WineCellar at any time without waiting for the next release cycle. WineCellar is heavily based on the ideas of Wine-Doors(Seem to be dead) and PlayOn Linux, combining the best from both projects. """,
    cmdclass={'install': InstallAndUpdateDataDirectory, 'remove': Remove} 
    #
    #This next part it for the Cheese Shop, look a little down the page.
    #classifiers = []     
)
